<?php 
/**
 * 预购商品处理
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: bonus.php 17217 2011-01-19 06:29:08Z liubo $
*/


define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'list';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}


/* 预购商品列表 */
if( $_REQUEST['act'] == "list"){
    $smarty->assign('ur_here', "预购商品列表");
    $smarty->assign('action_link', array('href' => 'pre_order.php?act=add', 'text' => "添加预购商品"));
    $smarty->assign('full_page',    1);
    //获取预购商品列表
    $pre_order_list=get_pre_order_list();
    $smarty->assign('agency_list',  agency_list());//办事处列表
    $smarty->assign('pre_order_list',   $pre_order_list['goods']);
    $smarty->assign('filter',       $pre_order_list['filter']);
    $smarty->assign('record_count', $pre_order_list['record_count']);
    $smarty->assign('page_count',   $pre_order_list['page_count']);
    $smarty->display('pre_order_list.htm');
}

//订单列表分页
elseif ($_REQUEST['act'] == 'query')
{
    /* 检查权限 */
    admin_priv('pre_order_manage');
    $pre_order_list = get_pre_order_list();//订单列表
    $smarty->assign('pre_order_list',   $pre_order_list['goods']);
    $smarty->assign('filter',       $pre_order_list['filter']);
    $smarty->assign('record_count', $pre_order_list['record_count']);
    $smarty->assign('page_count',   $pre_order_list['page_count']);

    make_json_result($smarty->fetch('pre_order_list.htm'), '', array('filter' => $pre_order_list['filter'], 'page_count' => $pre_order_list['page_count']));
}



//添加预购商品
if( $_REQUEST['act'] == "add"){
    $smarty->assign('agency_list',  agency_list());//办事处列表

    if($_REQUEST['step'] == "complete"){
        $area_id            = 3;    //区域ID（写死了，本来要传过来的）
        $goods_id           = $_REQUEST['goods_id'];   //商品ID
        $goods_sku          = $_REQUEST['goods_sku'];  //商品编号
        $pre_order_num      = $_REQUEST['pre_order_num']; //预购数量
        $limit_num          = $_REQUEST['limit_num'];     //限制数量
        $buy_number         = $_REQUEST['buy_number'];    //剩余件数
        $goods_num          = $_REQUEST['goods_num'];     //商品库存
        $pre_order_part     = $_REQUEST['pre_order_part']; //商品显示所在区域
        $pre_order_price    = $_REQUEST['pre_order_price']; //预购价格
        $promote_start_date = strtotime($_REQUEST['promote_start_date']); //开始时间
        $promote_end_date   = strtotime($_REQUEST['promote_end_date']);   //结束时间
        //重组数据
        foreach ($goods_sku as $key => $value) {
            $pre_order_goods[$key]['goods_sku'] = $value;
            $pre_order_goods[$key]['area_id'] = 3;
            $pre_order_goods[$key]['goods_id'] = $goods_id[$key];
            $pre_order_goods[$key]['goods_num'] = $goods_num[$key];
            $pre_order_goods[$key]['pre_order_num'] = $pre_order_num[$key];
            $pre_order_goods[$key]['limit_num'] = $limit_num[$key];
            $pre_order_goods[$key]['buy_number'] = $buy_number[$key];
            $pre_order_goods[$key]['pre_order_part'] = $pre_order_part[$key];
            $pre_order_goods[$key]['pre_order_price'] = $pre_order_price[$key];
        }

        //写入数据
        foreach ($pre_order_goods as $key => $value) {
            //插入预购表记录
            $sql = ' INSERT INTO ' . $ecs->table('pre_order') . ' (goods_id,goods_sku,goods_num,pre_order_num,limit_num,area_id,pre_order_part,start_time,end_time,pre_order_price,buy_number) ' . 
                    " VALUES('$value[goods_id]','$value[goods_sku]','$value[goods_num]','$value[pre_order_num]','$value[limit_num]','$value[area_id]','$value[pre_order_part]','$promote_start_date','$promote_end_date','$value[pre_order_price]','$value[buy_number]') ";
            $db->query($sql);   
            //重组预购数据
            $goods[$value['goods_id']]['pre_order_price'] = $value['pre_order_price'];
            $goods[$value['goods_id']]['promote_start_date'] = $promote_start_date;
            $goods[$value['goods_id']]['promote_end_date'] = $promote_end_date;
        }

        //更新预购商品
        // foreach ($goods as $key => $value) {
        //  $sql =  " UPDATE ".$GLOBALS['ecs']->table('goods').
        //          " set promote_price=$value[promote_price],promote_start_date = $value[promote_start_date] ,promote_end_date = $value[promote_end_date],is_promote=1 ".
        //          " WHERE goods_id = $key ";
        //  $db->query($sql);
        // }
        header("Location: pre_order.php?act=list");
    }
    $smarty->display('pre_order_add.htm');
}

//获取预购商品
if($_REQUEST['act'] == "add_goods"){

    $keyword=$_REQUEST['keyword'];
    $area_id=3;//写死，因为现在只有网仓一个地方发货
    $where .= " AND (g.goods_id LIKE '%" . mysql_like_quote($keyword) . "%' OR gs.goods_sku LIKE '%" . mysql_like_quote($keyword) . "%' OR g.goods_name LIKE '%" . mysql_like_quote($keyword) . "%')";
    if(!empty($area_id)){
        $where .= " AND gs.area_id = $area_id";
    }

    $sql=" SELECT g.goods_id,g.goods_name,gs.area_id, ga.agency_name,gs.goods_number,g.shop_price,g.promote_start_date,g.promote_end_date,g.goods_sku ".
         " FROM ".$GLOBALS['ecs']->table('goods') ." AS g ".
         " LEFT JOIN ". $GLOBALS['ecs']->table('goods_stock') ." AS gs ON g.goods_sku = gs.goods_sku ".
         " LEFT JOIN ". $GLOBALS['ecs']->table('agency') ." AS ga ON agency_id = gs.area_id ".
         " WHERE  g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".$where;

    $goods_list=$db->getAll($sql);
    $smarty->assign('goods_list',   $goods_list);
    $return=$smarty->fetch('pre_order_add_goods.htm');
    echo json_encode($return);
    exit();
}

//编辑预购商品
if($_REQUEST['act'] == "edit_pre_order"){
    $pre_order_num = !empty($_REQUEST['pre_order_num'])?$_REQUEST['pre_order_num']:"0"; //预购数量
    $limit_num = !empty($_REQUEST['limit_num'])?$_REQUEST['limit_num']:"0"; //限制数量
    $pre_order_id  = !empty($_REQUEST['pre_order_id'])?$_REQUEST['pre_order_id']:"0"; //预购ID
    $pre_order_price = !empty($_REQUEST['pre_order_price'])?$_REQUEST['pre_order_price']:"0"; //预购价
    $buy_number = !empty($_REQUEST['buy_number'])?$_REQUEST['buy_number']:"0"; //剩余数量
    $pre_order_part = !empty($_REQUEST['pre_order_part'])?$_REQUEST['pre_order_part']:"0"; //部分
    $sort = !empty($_REQUEST['sort'])?$_REQUEST['sort']:"0"; //部分
    
    $return['error'] = 0;
    $return['content'] = "";

    //查询原预购信息
    $sql ="SELECT * FROM ".$GLOBALS['ecs']->table('pre_order') ." WHERE pre_order_id = $pre_order_id AND is_stock = 0 ";
    $pre_order_info=$db->getRow($sql);
    if(empty($pre_order_info)){
        $return['error'] = 2;
        $return['content'] = "该预购商品已结算或不存在"; 
        echo json_encode($return);
        exit();
    }
    /* 比较修改 */
    //修改其他信息
    $sql =  " UPDATE ".$GLOBALS['ecs']->table('pre_order').
                    " set pre_order_num=$pre_order_num , limit_num = $limit_num , pre_order_price=$pre_order_price , buy_number = $buy_number , pre_order_part = $pre_order_part, sort = $sort ".
                    " WHERE pre_order_id = $pre_order_id";

    $db->query($sql); 

    //修改价格信息
    // $sql =  " UPDATE ".$GLOBALS['ecs']->table('goods').
    //                 " set promote_price=$promote_price ".
    //                 " WHERE goods_sku = $pre_order_info[goods_sku] ";
    //                     $return['error'] = 1;
    // $db->query($sql); 
    $return['error'] = 1;
    $return['content'] = "修改信息成功"; 
    echo json_encode($return);
    exit();

}


/* 删除预购库存 */
if($_REQUEST['act'] == "delete_stock"){
    $pre_order_id  = !empty($_REQUEST['pre_order_id'])?$_REQUEST['pre_order_id']:""; //预购ID
    $sql ="SELECT * FROM ".$GLOBALS['ecs']->table('pre_order') ." WHERE pre_order_id = $pre_order_id AND is_stock = 0 ";
    $pre_order_id_info=$db->getRow($sql);
    if(empty($pre_order_id_info)){
        $return['error'] = 2;
        $return['content'] = "该记录不存在"; 
        echo json_encode($return);
        exit();
    }
    //更新闪购信息
    // $sql =  " UPDATE ".$GLOBALS['ecs']->table('goods').
    //         " set promote_price=0,promote_start_date = 0 ,promote_end_date = 0,is_promote=0".
    //         " WHERE goods_id = $pre_order_id_info[goods_id] ";
    // $db->query($sql);
    //删除预购信息
    $sql =  " DELETE FROM".$GLOBALS['ecs']->table('pre_order').
            " WHERE pre_order_id = $pre_order_id ";
    $db->query($sql);
    $return['error'] = 1;
    $return['content'] = "删除成功"; 
    echo json_encode($return);
    exit();
}



//获取预购商品列表
function get_pre_order_list(){
        $filter['promote_start_date']        = empty($_REQUEST['promote_start_date']) ? '' : strtotime($_REQUEST['promote_start_date']);//缺货筛选
        $filter['promote_end_date']          = empty($_REQUEST['promote_end_date']) ? '' : strtotime($_REQUEST['promote_end_date']);
        $filter['area_id']                   = empty($_REQUEST['area_id']) ? 0 : intval($_REQUEST['area_id']);
        $filter['keyword']                   = empty($_REQUEST['keyword']) ? "" : $_REQUEST['keyword'];
        $where = 'WHERE g.is_delete = 0 ';
        /* 关键字 */
        if (!empty($filter['keyword']))
        {
            $where .= " AND (g.goods_id LIKE '%" . mysql_like_quote($filter['keyword']) . "%' OR gs.goods_sku LIKE '%" . mysql_like_quote($filter['keyword']) . "%' OR g.goods_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%')";
        }
        /*地区*/
        if(!empty($filter['area_id'])){
             $where .=" AND po.area_id = $filter[area_id] ";
        }
        /*时间*/
        if(!empty($filter['promote_start_date'])&&!empty($filter['promote_end_date'])){
            $where .=" AND po.start_time >= $filter[promote_start_date]  AND po.start_time <= $filter[promote_end_date]";
        }
        /* 分页大小 */
        $filter['page'] = empty($_REQUEST['page']) || (intval($_REQUEST['page']) <= 0) ? 1 : intval($_REQUEST['page']);
        if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0)
        {
            $filter['page_size'] = intval($_REQUEST['page_size']);
        }
        elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0)
        {
            $filter['page_size'] = intval($_COOKIE['ECSCP']['page_size']);
        }
        else
        {
            $filter['page_size'] = 15;
        }
        //查询总页数
        $sql = " SELECT g.goods_id,g.goods_name,g.goods_sku,sa.area_name,gs.goods_number,po.buy_number,po.pre_order_price,po.pre_order_id,po.is_stock,po.goods_num,po.limit_num,po.buy_num,po.car_num,po.start_time ,po.pre_order_id, po.end_time, po.pre_order_num, po.pre_order_part, po.sort ".
                " FROM ". $GLOBALS['ecs']->table('pre_order') ."AS po".
                " LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g ON g.goods_id=po.goods_id".
                " LEFT JOIN ".$GLOBALS['ecs']->table('goods_stock')." AS gs ON gs.goods_id=po.goods_id AND gs.area_id = po.area_id".
                " LEFT JOIN ".$GLOBALS['ecs']->table('service_area')." AS sa ON sa.area_id=po.area_id ".
                $where.
                " ORDER BY po.pre_order_id DESC";
        $filter['record_count'] = $GLOBALS['db']->num_rows($GLOBALS['db']->query($sql)); // 总数
        $filter['page_count']   = $filter['record_count'] > 0 ? ceil($filter['record_count'] / $filter['page_size']) : 1; // 总页码
        $sql .= " LIMIT " . ($filter['page'] - 1) * $filter['page_size'] . ",$filter[page_size]"; // 调取页码数量

        $row = $GLOBALS['db']->getAll($sql);
        //格式化数据
        foreach ($row as $key => $value) {
            $row[$key]['promote_start_date'] = date('Y-m-d-H:s',$value['start_time']);
            $row[$key]['promote_end_date']   = date('Y-m-d-H:s',$value['end_time']);
            $row[$key]['over_time']          = (time()<$row[$key]['end_time'])?0:1;
        }
        $arr = array('goods' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
        return $arr;
}

 ?>